@extends('layouts.admin')
@section('style')
    <style type="text/css">
        .box_form .layui-input-inline{
            width:400px;
            min-width:200px;
        }
        .layui-form-select dl{
            z-index:99999;
        }
    </style>
@endsection
@section('content')
    <div class="admin_main">
        <div class="admin_header_fixed">
            <div class="admin_header clearfix">
                <div class="title"><i></i>新增订单</div>
                <div class="action">
                    <b></b>
                </div>
            </div>
        </div>
        <div class="admin_base">
            <div class="box_form">
                <div class="box_content">
                    <form method="post" action="{{ url('order') }}" id="VoForm" class="layui-form" novalidate="1">
                        {{ csrf_field() }}
                        <fieldset class="layui-elem-field">
                            <legend style="font-size:16px;margin-bottom:10px">基本信息</legend>
                            <div class="layui-row">
                                <div class="layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>所属店铺</label>
                                        <div class="layui-input-inline">
                                            <select name="shop_id" id="shop_id" style="z-index:9999;" lay-filter="shop_id">
                                                <option value="">请选择所属店铺</option>
                                                @foreach($shops as $shop)
                                                    <option value="{{ $shop->id }}">{{ $shop->title }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>选择宝贝</label>
                                        <div class="layui-input-inline">
                                            <select name="iid" id="iid" style="z-index:9999;" lay-filter="iid"></select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">宝贝ID</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="item_id" placeholder="请输入宝贝ID" value="" id="item_id"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">宝贝标题</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="item_title" placeholder="请输入宝贝标题" value="" id="item_title"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>宝贝价格</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="price" id="price" placeholder="请输入宝贝价格" value="0"
                                                   id="seller_name" class="layui-input">
                                        </div>
                                   </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>佣金</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="commission" id="commission" placeholder="请输入佣金" value="0"
                                                   id="seller_name" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">&nbsp;</label>
                                        <div class="layui-input-block admin_bottom" style="">
                                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save">
                                                立即提交
                                            </button>
                                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md6"  style="border-left:1px solid #ececec;">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>买家账号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="account_no" placeholder="请输入买家账号" value=""
                                                   id="account_no" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>订单编号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="order_no" placeholder="请输入订单编号" value=""
                                                   id="order_no" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">订单时间</label>
                                        <div class="layui-input-block">
                                            <input type="text" class="layui-input" readonly="readonly" name="order_time" id="order_time" value="{{date('Y-m-d H:i:s',time())}}" style="width:200px;" />
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>实转金额</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="money_sj" id="money_sj" placeholder="请输入实际转账金额" value="0" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><span>*</span>其中佣金</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="money_sj" id="commission_sj" placeholder="请输入实转金额中佣金金额" value="0" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">备注</label>
                                        <div class="layui-input-block">
                                            <textarea name="comm" class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
        <div class="admin_bottom"></div>
    </div>
@endsection
@section('script')
    <script type="text/javascript" src="{{ asset('files/ueditor/ueditor.config.js') }}"></script>
    <script type="text/javascript" src="{{ asset('files/ueditor/ueditor.all.js') }}"></script>
    <script type="text/javascript" src="{{ asset('files/ueditor/lang/zh-cn/zh-cn.js') }}"></script>
    <script type="text/javascript">
        var goods_url = "{{ url("goods/list") }}";
        var add_iid = null;
        layui.use(['form','laydate', 'element', 'layer', 'utils', 'formField'], function () {
            var form = layui.form;
            var element = layui.element;
            var URL = layui.utils.URL;
            var errorTip = layui.utils.errorTip;
            var $ = layui.$;
            var laydate = layui.laydate;
            var layer = layui.layer;
            var temp = laydate.render({
                elem:'#order_time',
                type:'datetime',
                change:function(value,date,endDate){
                }
            })
            add_iid = function(datas){
                var _html="<option value=''>选择宝贝</option>";
                console.log(datas)
                for(var i in datas){
                    _html += '<option data-price="'+datas[i].price+'" data-commission="'+datas[i].commission+'" data-title="'+datas[i].item_title+'" data-id="'+datas[i].item_id+'" value="'+datas[i].id+'">'+datas[i].item_title+'</option>';
                }
                return _html;
            }
            form.on('select(shop_id)',function(data){
                 var _shop_id = data.value;
                 $("#iid").html("");
                 $("#item_id").val("");
                 $("#item_title").val("");
                 var _data = {id:_shop_id}
                 if(_shop_id>0){
                     URL.get(goods_url,_data,{
                         dataType:'json',
                         success:function(res){
                             var _datas = res.data;
                             var _html = add_iid(_datas);
                             $("#iid").html(_html);
                             form.render('select');
                         }
                     })
                 }
            });
            form.on('select(iid)',function(data){

                var _option  =  $(data.elem).find('option:selected');
                $("#item_id").val(_option.data('id'));
                $("#item_title").val(_option.data('title'));
                $("#price").val(_option.data('price'));
                $("#commission").val(_option.data('commission'));
            });
            form.on('submit(save)', function (data) {
                var _check_url = "{{ url('order/check') }}";
                var account_no = $("#account_no").val();
                var shop_id = $("#shop_id").val();
                if(shop_id==''){
                    layer.msg('请选择店铺', {icon: 5, times: 2000});
                    return false;
                }
                if(account_no==''){
                    layer.msg('请填写买家账号', {icon: 5, times: 2000});
                    return false;
                }
                var url = $(data.form).prop('action');
                var data = data.field;
                URL.post(_check_url,{
                    account_no:account_no,
                    shop_id:shop_id
                },{
                    dataType:'json',
                    success:function(res){
                        if(res.code==1){
                            doSave(url,data,function(){})
                        }else{
                            if(res.data.tiptype==2 || res.data.tiptype==0){
                                parent.layer.alert(res.message);
                            }else{
                                parent.layer.confirm(res.message,{
                                    title:'异常提示',
                                    btn:['确认保存','取消'],
                                    yes:function(index){
                                        doSave(url,data,function(){
                                            parent.layer.close(index)
                                        })
                                    }
                                })
                            }
                        }
                    }
                })
                return false;
            })
            function doSave(url,data,callback){
                URL.post(url, data, {
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg(res.message, {icon: 6, times: 2000}, function () {
                                document.location.reload();
                            })
                        } else {
                            var errors = res.errors;
                            if (typeof errors != 'undefined') {
                                errorTip(errors);
                            } else {
                                layer.msg(res.message, {icon: 5, times: 2000});
                            }
                        }
                        callback();
                    }
                })
            }
        });
    </script>
@endsection